package com.meihua.shellapp.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.adapter.OnItemClickListener;
import com.meihua.shellapp.adapter.personalcenter.MessageLVAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.MessageListBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 消息界面
 */
public class MessageActivity extends BaseActivity implements OnItemClickListener {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.message_iRecyclerView)
    IRecyclerView mRecyclerView;
    private List<MessageListBean.DataBean> mData;
    private MessageLVAdapter rvAdapter;
    private int mPage = 1;
    private LoadMoreFooterView loadMoreFooterView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("消息");
        mIVTopLeft.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) mRecyclerView.getLoadMoreFooterView();
        mData = new ArrayList<>();
        rvAdapter = new MessageLVAdapter(mData);
        mRecyclerView.setIAdapter(rvAdapter);
        mRecyclerView.setOnLoadMoreListener(this);
        mRecyclerView.setOnRefreshListener(this);
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        getData();
    }


    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && rvAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkGo.<String>post(URL.getUrl().GETMESSAGES)
                .params("page", mPage)
                .params("mid", MyApplication.getInstance().getMId())
                .execute(new MyStringCallBack<MessageListBean>(mRecyclerView) {
                    @Override
                    public void onSuccessData(MessageListBean bean) {
                        super.onSuccessData(bean);
                        if (null != bean.getData() && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            rvAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                            endRefreshing(mRecyclerView);
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getData();
    }

    /*条目点击*/
    @Override
    public void onItemClick(int position, View v) {
        /*详情页*/
        startActivity(new Intent(this, WebActivity.class));
    }

    @OnClick(R.id.iv_top_left)
    public void onViewClicked() {
        finish();
    }
}
